﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWeb.ServiceBus.Model;
using MyWeb.ServiceBus.Model.Dto;
using MyWeb.ServiceBus.Model.Models;
using MyWeb.ServiceBus.Services.Db;
using System.Drawing.Printing;

namespace MyWeb.ServiceBus.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RequestLogController : ControllerBase
    {
        private Context db;
        public RequestLogController(Context context)
        {
            db = context;
        }

        [HttpPost]
        public async Task<ApiResult> Get(RequestLogQuery query)
        {
            if(query.RequstTime is null || query.RequstTime.Count < 2)
            {
                query.RequstTime = new List<DateTime>
                {
                    DateTime.Now.AddDays(-1),
                    DateTime.Now
                };
            }
            if(query.Page <= 0)
            {
                query.Page = 1;
            }
            if (query.PageSize <= 0)
            {
                query.PageSize = 1;
            }


            var Exec = from a in db.RequestLog
                       join b in db.SysService on a.SysServiceId equals b.Id
                       join c in db.Application on a.ApplicationId equals c.Id
                       join d in db.Application on a.CallerAppId equals d.Id
                       where a.RearStartTime >= query.RequstTime[0]
                          && a.RearStartTime <= query.RequstTime[1]
                          && c.Code.Contains(query.AppCode)
                          && b.Code.Contains(query.ServiceCode)
                          && (a.EsbCostTime + a.CostTime >= query.TakeUpStartTime || query.TakeUpStartTime == 0)
                          && (a.EsbCostTime + a.CostTime <= query.TakeUpEndTime || query.TakeUpEndTime == 0)
                       select new
                       {
                           Id = a.Id,
                           ServiceCode = b.Code,//服务编码
                           SerAppCode = c.Code,//调用方应用编码
                           CallerAppCode = d.Code,//服务方应用编码
                           RequsetTime = a.RearStartTime,//请求时间
                           ResponTime = a.RearEndTime,//响应时间
                           EsbCostTime = a.EsbCostTime,//前端请求耗时
                           CostTime = a.EsbCostTime + a.CostTime//后端请求耗时
                       };

            long count = Exec.LongCount();

            var list = Exec.Skip((query.Page - 1) * query.PageSize)
                         .Take(query.PageSize);


            var pageData = new Model.PageModel(query.Page, (int)count, query.PageSize, list);

            return ApiResult.SUCCESS(pageData);
        }

        [HttpGet]
        public async Task<ApiResult> GetInfo(long logId)
        {
            var data = db.RequestLog
                .Join(db.SysService, a => a.SysServiceId, b => b.Id, (a, b) => new { a, b })
                .Join(db.Application, a => a.a.ApplicationId, b => b.Id, (a, b) => new { a, b })
                .Join(db.Application, a => a.a.a.CallerAppId, b => b.Id, (a, b) => new { a, b })
                .Join(db.Gateway, a => a.a.a.a.GatewayId, b => b.Id, (a, b) => new { a, b })
                .Where(a => a.a.a.a.a.Id == logId)
                .Select((a, b) => new RequestLogInfo()
                {
                    ApplicationId = a.a.a.a.a.ApplicationId,
                    SysServiceId = a.a.a.a.a.SysServiceId,
                    GatewayId = a.a.a.a.a.GatewayId,
                    CallerAppId = a.a.a.a.a.CallerAppId,
                    Headers = a.a.a.a.a.Headers,
                    Response = a.a.a.a.a.Response,
                    UrlReferrer = a.a.a.a.a.UrlReferrer,
                    RequestParam = a.a.a.a.a.RequestParam,
                    ResultParam = a.a.a.a.a.ResultParam,
                    RequestLen = a.a.a.a.a.RequestLen,
                    ResultLen = a.a.a.a.a.ResultLen,
                    ForeStartTime = a.a.a.a.a.ForeStartTime,
                    ForeEndTime = a.a.a.a.a.ForeEndTime,
                    RearStartTime = a.a.a.a.a.RearStartTime,
                    RearEndTime = a.a.a.a.a.RearEndTime,
                    Version = a.b.Version,
                    ServiceCode = a.a.a.a.b.Code,//服务编码
                    SerAppCode = a.a.a.b.Code,//调用方应用编码
                    CallerAppCode = a.a.b.Code//服务方应用编码
                }).Single();
            data.EsbCostTime = (data.ForeEndTime - data.ForeStartTime).TotalMilliseconds;
            data.CostTime = (data.RearEndTime - data.RearStartTime).TotalMilliseconds;
            return ApiResult.SUCCESS(data);
        }
        [HttpGet]
        public ApiResult Test()
        {
            var data = db.RequestLog.Where(r => r.Id == 1696790720196775939).Single();


            Console.WriteLine();//前端请求耗时
            Console.WriteLine((data.RearEndTime - data.RearStartTime).TotalMilliseconds);

            return ApiResult.SUCCESS(data);
        }
    }
}
